package com.examination.system.administrator.service.impl;

import com.alibaba.fastjson.JSON;
import com.examination.framework.model.response.CommonCode;
import com.examination.framework.model.response.QueryResponseResult;
import com.examination.framework.model.response.QueryResult;
import com.examination.system.administration.Attendance;
import com.examination.system.administration.Employee;
import com.examination.system.administration.ext.QueryAttendance;
import com.examination.system.administration.response.NationCode;
import com.examination.system.administration.statistics.*;
import com.examination.system.administrator.dao.AttendanceMapper;
import com.examination.system.administrator.dao.EmployeeMapper;
import com.examination.system.administrator.service.AttendanceService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author 寇淇芮
 * @Date 2019/10/10 18:42
 */
@Service
public class AttendanceServiceImpl implements AttendanceService {
    @Autowired
    AttendanceMapper  attendanceMapper;
    @Autowired
    EmployeeMapper employeeMapper;
    @Override
    public QueryResponseResult findDataAll() {
        return new QueryResponseResult(CommonCode.SUCCESS,new QueryResult());
    }

    @Override
    public QueryResponseResult findQueryAttendance(QueryAttendance queryAttendance) {
        switch (queryAttendance.getType()){
            case "1":
                return this.getPieChart(queryAttendance);
            case "2":
                return this.getBrokenLinkCahar();
            default:

        }
        return null;
    }

    private QueryResponseResult<BrokenLineChart> getBrokenLinkCahar() {
        List<BrokenLineChart> lineChart = new ArrayList<>();
        BrokenLineChart brokenLineChart = new BrokenLineChart();
        List<String> lineChartData = new ArrayList<>();
        lineChartData.add("加班");
        lineChartData.add("迟到");
        lineChartData.add("缺勤");
        lineChartData.add("早退");
        brokenLineChart.setLineChartData(lineChartData);
        //获取数据
        List<Attendance> attendanceList =  attendanceMapper.findAttendancCountGroupByThisData();
        List<String> lineChartXAxis = new ArrayList<>();

        List<Integer> overtimeList =  new ArrayList<>();
        List<Integer> lateList =  new ArrayList<>();
        List<Integer> absentList =  new ArrayList<>();
        List<Integer> leaveEarlyList =  new ArrayList<>();

        for(Attendance item : attendanceList){
            Calendar instance = Calendar.getInstance();
            instance.setTime(item.getThisData());
            Integer date = instance.get(Calendar.DATE);
            lineChartXAxis.add(date.toString());

            overtimeList.add(item.getOvertime());
            lateList.add(item.getLate());
            absentList.add(item.getAbsent());
            leaveEarlyList.add(item.getLeaveEarly());
        }
        //封装数据
        brokenLineChart.setLineChartXAxis(lineChartXAxis);
        List<BrokenLineData> seriesData = new ArrayList<>();
        BrokenLineData brokenLineData = new BrokenLineData();
        brokenLineData.setName(lineChartData.get(0));
        brokenLineData.setType("line");
        brokenLineData.setStack("总和");
        brokenLineData.setData(overtimeList);
        seriesData.add(brokenLineData);
        brokenLineData = new BrokenLineData();
        brokenLineData.setName(lineChartData.get(1));
        brokenLineData.setType("line");
        brokenLineData.setStack("总和");
        brokenLineData.setData(lateList);
        seriesData.add(brokenLineData);
        brokenLineData = new BrokenLineData();
        brokenLineData.setName(lineChartData.get(2));
        brokenLineData.setType("line");
        brokenLineData.setStack("总和");
        brokenLineData.setData(absentList);
        seriesData.add(brokenLineData);
        brokenLineData = new BrokenLineData();
        brokenLineData.setName(lineChartData.get(3));
        brokenLineData.setType("line");
        brokenLineData.setStack("总和");
        System.out.println(JSON.toJSONString(leaveEarlyList));
        brokenLineData.setData(leaveEarlyList);
        seriesData.add(brokenLineData);
        brokenLineChart.setSeriesData(seriesData);

        QueryResult queryResult = new  QueryResult();
        lineChart.add(brokenLineChart);
        queryResult.setList(lineChart);
        return new QueryResponseResult(CommonCode.SUCCESS, queryResult);

    }

    /**
     * 饼图数据
     * @return
     */
    private QueryResponseResult<PieChart> getPieChart(QueryAttendance queryAttendance) {
        List<PieChart> listPie = new ArrayList<>();
        PieChart pieChart = new PieChart();
        List<String> pieLegendData = new ArrayList<>();
        pieLegendData.add("加班");
        pieLegendData.add("迟到");
        pieLegendData.add("缺勤");
        pieLegendData.add("早退");


        Employee employee = queryAttendance.getEmployee();


        List<DataChart> seriesData =  new ArrayList<>();
        //
        Attendance attendancCount = attendanceMapper.findAttendancCount(employee);
        seriesData.add(new DataChart(pieLegendData.get(0),attendancCount.getOvertime()));
        seriesData.add(new DataChart(pieLegendData.get(1),attendancCount.getLate()));
        seriesData.add(new DataChart(pieLegendData.get(2),attendancCount.getAbsent()));
        seriesData.add(new DataChart(pieLegendData.get(3),attendancCount.getLeaveEarly()));
        pieChart.setPieLegendData(pieLegendData);
         pieChart.setSeriesData(seriesData);
        QueryResult queryResult = new  QueryResult();
        listPie.add(pieChart);
        queryResult.setList(listPie);
        return new QueryResponseResult(CommonCode.SUCCESS, queryResult);
    }
}
